//
//  FirstViewController.m
//  twittsTab2
//
//  Created by Steven Hamilton on 2/25/10.
//  Copyright __MyCompanyName__ 2010. All rights reserved.
//

#import "FirstViewController.h"
#import "TwitterRequest.h"
#import "twittsTableViewController.h"

@implementation FirstViewController



@synthesize usernameField;
@synthesize passwordField;
@synthesize loginButton;
@synthesize loginIndicator;


@synthesize tweetMood, tweetWeight, tweetCig, tweetGlucose, submitAll, timer;

//Submit tweet tab



- (void)status_updateCallback: (NSData *) content{
	return;
}
	
	//NSLog(@"%@", [[NSString alloc] initWithData:content encoding:NSASCIIStringEncoding]);
	



/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/

/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
}
*/

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

-(IBAction)login:(id)sender{
	TwitterRequest *tr = [[TwitterRequest alloc] init];
	NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
	
	NSString *username;
	NSString *password;
	
	username = usernameField.text;
	password = passwordField.text;
	
	if (standardUserDefaults) {
		[standardUserDefaults setObject:username forKey:@"username"];
		//[user synchronize];
		[standardUserDefaults setObject:password forKey:@"password"];
		[standardUserDefaults synchronize];
		
	}
	
	
	tr.username = username;
	tr.password = password;
	
	NSLog(@"user: %@",tr.username);

	[usernameField resignFirstResponder];
	[passwordField resignFirstResponder];
	
	
	//NSLog(@"Getting timeline for user %@", tr.username);
	[tr friends_timeline:self requestSelector:@selector(friends_timeline_callback:)];
	
	loginIndicator.hidden = FALSE;
	
	
	[loginIndicator startAnimating];
}

- (void) friends_timeline_callback:(NSData *)data{
		
	NSLog(@"Logged in");
	// inform the user that the user name and password
	// in the preferences are incorrect
	UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Welcome"
													 message:[NSString stringWithFormat:@"You have sucessfully logged in"]
													delegate:nil
										   cancelButtonTitle:@"Ok"
										   otherButtonTitles:nil];
	[alert show];
	[alert release];
	
	[loginIndicator stopAnimating];
	//NSLog(@"user 1: %@", userName);
	

}

- (IBAction)pressDoneKey:(UITextField *)textBoxName {
	[textBoxName resignFirstResponder];
	
}


-(IBAction) submitTweet:(id)sender{

	[tweetMood resignFirstResponder];
	[tweetWeight resignFirstResponder];
	[tweetCig resignFirstResponder];
	[tweetGlucose resignFirstResponder];
	
	
	[self submitMood];
	[self submitWeight];
	[self submitCigs];
	[self submitGlucose];
	
}

-(void)submitMood{
	NSString *db;
	NSString *dbValue;
	NSString *tempString;
	NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
	NSString *username;
	NSString *password;
	TwitterRequest *tr = [[TwitterRequest alloc] init];
	if (standardUserDefaults){
		username = [standardUserDefaults objectForKey:@"username"];
		password = [standardUserDefaults objectForKey:@"password"];
	}
	
	if(![tweetMood.text isEqual:@""]){
	
		NSLog(@"user: %@",username);
		tr.username = username;
		tr.password = password;
	
		dbValue = @"m$";
		tempString = tweetMood.text;
		db = [dbValue stringByAppendingString:tempString];
		NSLog(@"mood: %@",db);
		[tr statuses_update:db];
	}
	[tr release];
}

-(void)submitWeight{
	NSString *db;
	NSString *dbValue;
	NSString *tempString;
	NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
	NSString *username;
	NSString *password;
	TwitterRequest *tr2 = [[TwitterRequest alloc] init];
	
	if (standardUserDefaults){
		username = [standardUserDefaults objectForKey:@"username"];
		password = [standardUserDefaults objectForKey:@"password"];
	}
	
	if(![tweetWeight.text isEqual:@""]){
		
		dbValue = @"w$";
		tempString = tweetWeight.text;
		db = [dbValue stringByAppendingString:tempString];
		NSLog(@"weight: %@",db);
		tr2.username = username;
		tr2.password = password;
		[tr2 statuses_update:db];
	}
	[tr2 release];
}

-(void)submitCigs{
	NSString *db;
	NSString *dbValue;
	NSString *tempString;
	NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
	NSString *username;
	NSString *password;
	TwitterRequest *tr = [[TwitterRequest alloc] init];
	
	if (standardUserDefaults){
		username = [standardUserDefaults objectForKey:@"username"];
		password = [standardUserDefaults objectForKey:@"password"];
	}
	
	if(![tweetCig.text isEqual:@""]){
		
		NSLog(@"user: %@",username);
		tr.username = username;
		tr.password = password;
		
		dbValue = @"c$";
		tempString = tweetCig.text;
		db = [dbValue stringByAppendingString:tempString];
		NSLog(@"mood: %@",db);
		[tr statuses_update:db];
	}
	
	[tr release];
}

-(void)submitGlucose{
	NSString *db;
	NSString *dbValue;
	NSString *tempString;
	NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
	NSString *username;
	NSString *password;
	TwitterRequest *tr = [[TwitterRequest alloc] init];
	
	if (standardUserDefaults){
		username = [standardUserDefaults objectForKey:@"username"];
		password = [standardUserDefaults objectForKey:@"password"];
	}
	
	if(![tweetGlucose.text isEqual:@""]){
		
		NSLog(@"user: %@",username);
		tr.username = username;
		tr.password = password;
		
		dbValue = @"g$";
		tempString = tweetGlucose.text;
		db = [dbValue stringByAppendingString:tempString];
		NSLog(@"mood: %@",db);
		[tr statuses_update:db];
	}
	
	[tr release];
}

	
- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}

@end
